package com.mf.daoImpl;

import java.lang.reflect.ParameterizedType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mf.dao.UserDao;
import com.mf.entity.User;
import com.mf.util.JdbcDaoHelper;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{

	   private Class<?> EntityClass;

	    private String sql;

	    private PreparedStatement statement;

	    private ResultSet rs;

	    private List<User> list;
	    
	    public UserDaoImpl() {

	        ParameterizedType type = (ParameterizedType) getClass()
	                .getGenericSuperclass();
	        EntityClass = (Class<?>) type.getActualTypeArguments()[0];
	    }

		@Override
	    public List<User> selectAll() {
			List<User> list=new ArrayList<User>();
			sql="SELECT * from realmf.user";
			User mUser=null;
			try {
				statement=JdbcDaoHelper.getPreparedStatement(sql);
				rs=statement.executeQuery();
				if (null!=rs) {
					while(rs.next()){
						mUser=new User();
						mUser.setEmail(rs.getString("email"));
						mUser.setId(rs.getInt("id"));
						mUser.setSex(rs.getString("sex"));
						mUser.setName(rs.getString("name"));
						mUser.setPwd(rs.getString("pwd"));
						list.add(mUser);
					}
					return list;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				JdbcDaoHelper.release(statement, rs);
				}
			
			return null;
		}
	    
		@Override
		public User selectUserbyEmail(String email) {
			sql="SELECT * FROM realmf.user where email=? ;";
			User mUser=null;
			try {
				statement=JdbcDaoHelper.getPreparedStatement(sql);
				statement.setString(1, email);
				rs=statement.executeQuery();
				if (null!=rs) {
					while(rs.next()){
						mUser=new User();
						mUser.setEmail(email);
						mUser.setId(rs.getInt("id"));
						mUser.setSex(rs.getString("sex"));
						mUser.setName(rs.getString("name"));
						mUser.setPwd(rs.getString("pwd"));
					}
					if (null==mUser||mUser.getEmail().isEmpty()) {
						return null;
					}else {
						return mUser;
					}
				}

				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				JdbcDaoHelper.release(statement, rs);
				}
			
			return null;
		}

		@Override
		public boolean chackUserEmailIsUsed(String email) {
			sql="SELECT * FROM realmf.user where email=? ;";
			User mUser=null;
			try {
				statement=JdbcDaoHelper.getPreparedStatement(sql);
				statement.setString(1, email);
				rs=statement.executeQuery();
				if (null!=rs) {
					while(rs.next()){
						mUser=new User();
						mUser.setEmail(email);
						mUser.setId(rs.getInt("id"));
						mUser.setSex(rs.getString("sex"));
						mUser.setName(rs.getString("name"));
						mUser.setPwd(rs.getString("pwd"));
					}
					if (null==mUser||mUser.getEmail().isEmpty()) {
						return false;
					}else {
						return true;
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				JdbcDaoHelper.release(statement, rs);
				}
			
			
			return false;
		}
	
}
